package purchase

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// 融资费用

func Financing(r *gin.RouterGroup) {
	product := r.Group("/financing")
	{
		product.POST(sql.RequestAdd, financingAdd)
		product.POST(sql.RequestUpdate, financingUpdate)
		product.GET(sql.RequestList, financingList)
		product.GET(sql.RequestData, financingData)
	}
	financing := r.Group("/financingFee")
	{
		financing.POST(sql.RequestAdd, financingFeeAdd)
		financing.POST(sql.RequestUpdate, financingFeeUpdate)
		financing.GET(sql.RequestList, financingFeeList)
		financing.GET(sql.RequestData, financingFeeData)
		financing.GET(sql.RequestDelete, financingFeeDelete)
	}

}

func financingAdd(c *gin.Context) {
	sql.HttpAdd(c, &model.Financing{}, nil)
}

func financingUpdate(c *gin.Context) {
	sql.HttpUpdates(c, &model.Financing{}, nil)
}

func financingData(c *gin.Context) {
	sql.HttpData(c, &model.Financing{}, func(db *gorm.DB) *gorm.DB {
		if containerNO := c.Query("containerNO"); containerNO != "" {
			db = db.Where("container_no = ?", containerNO)
		} else {
			db.Error = errors.New(sql.ErrorParams)
		}
		return db
	})
}

func financingList(c *gin.Context) {
	sql.HttpList(c, &model.Financing{}, nil)
	/*sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var list []model.Financing
		db.Find(&list)
		for _, financing := range list {
			var product model.PurchaseProduct
			db.Where("container_no = ?", financing.ContainerNo).
				Preload("FuturesFee").
				Preload("TaxFee"). //税款
				First(&product)

			balanceFee := utils.Find(product.FuturesFee, func(t model.FuturesFee) bool {
				return t.FeeType == "实际尾款"
			})

			if balanceFee != nil && financing.BalanceInterest != 0 {
				var balance = model.FinancingFee{
					PurchaseProductId: financing.PurchaseProductId,
					ContainerNo:       financing.ContainerNo,
					FinancingRate:     financing.FinancingRate,
					FinancingAmount:   financing.BalanceDifference,
					FinancingDays:     financing.BalanceDays,
					FinancingInterest: financing.BalanceInterest,
					FinancingType:     "尾款",
					StartDate:         balanceFee.Date,
					EndDate:           financing.SettlementDate,
				}
				db.Create(&balance)
			}

			if product.TaxFee != nil && product.TaxFee.Date != "" && financing.TaxInterest != 0 {
				var tax = model.FinancingFee{
					PurchaseProductId: financing.PurchaseProductId,
					ContainerNo:       financing.ContainerNo,
					FinancingRate:     financing.FinancingRate,
					FinancingAmount:   financing.TaxDifference,
					FinancingInterest: financing.TaxInterest,
					FinancingDays:     financing.TaxDays,
					FinancingType:     "税款",
					StartDate:         product.TaxFee.Date,
					EndDate:           financing.SettlementDate,
				}
				db.Create(&tax)
			}

		}
		return db, nil
	})*/

}

func financingFeeAdd(c *gin.Context) {
	sql.HttpAdd(c, &model.FinancingFee{}, nil)
}

func financingFeeUpdate(c *gin.Context) {
	sql.HttpUpdates(c, &model.FinancingFee{}, nil)
}
func financingFeeList(c *gin.Context) {
	sql.HttpList(c, &[]model.FinancingFee{}, func(db *gorm.DB) *gorm.DB {
		if purchaseId := c.Query("purchaseId"); purchaseId != "" {
			db = db.Where("purchase_product_id = ?", purchaseId)
		}
		if containerNo := c.Query("containerNo"); containerNo != "" {
			db = db.Where("container_no = ?", containerNo)
		}
		return db
	})
}
func financingFeeDelete(c *gin.Context) {
	sql.HttpDeleteById(c, &model.FinancingFee{})
}
func financingFeeData(c *gin.Context) {
	sql.HttpDataById(c, &model.FinancingFee{})
}
